package com.googlecode.monitoring.web;


import com.googlecode.monitoring.db.dao.XmlDbAgentStatusDao;
import com.googlecode.monitoring.model.ItemToCheck;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public final class DbAgentStatusBean extends StatusBean {
    private List<? extends ItemToCheck> dbItems = new ArrayList<ItemToCheck>();
    Logger log = Logger.getLogger(DbAgentStatusBean.class);

	private XmlDbAgentStatusDao statusDao;

    protected DbAgentStatusBean() {
    }

    public DbAgentStatusBean(XmlDbAgentStatusDao agentStatusDao, SettingsBean settings) throws IOException {
        super(settings);
		this.statusDao = agentStatusDao;
    }
    
    @SuppressWarnings("unchecked")
    public List<ItemToCheck> getData() throws IOException {
        log.debug("reading db data");
        updateData(statusDao.getStatusResource());
        dbItems = statusDao.getAllItemsFromCheckReport();
        if (dbItems != null) {
            setOutdatedItemsCount(calculateOutdatedItemsCount(dbItems));
            Collections.sort(dbItems);
        } else {
            log.debug("data was not found");
            dataFound = false;
        }
        return dbItems == null ? Collections.EMPTY_LIST : dbItems;
    }

}
